package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/18
 * @description ：检查项业务实现类
 * @version: 1.0
 */
@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

	@Autowired
	private CheckItemDao checkItemDao;

	@Transactional
	@Override
	public void add(CheckItem checkItem) {
		log.debug("CheckItemServiceImpl:{}"+checkItem);
		checkItemDao.add((checkItem));
	}

	@Override
	public PageResult pageQuery(QueryPageBean pageBean) {
		// 使用分页插件PageHelper
		PageHelper.startPage(pageBean.getCurrentPage(),pageBean.getPageSize());
		// 获取分页数据
		Page<CheckItem> page = checkItemDao.selectByCondition(pageBean.getQueryString());
		// 封装分页数据，返回分页对象
		return new PageResult(page.getTotal(),page.getResult());
	}

	@Transactional
	@Override
	public void deleteById(Integer id) {
		Long count = checkItemDao.countCheckItemsById(id);
		if (count > 0){
			throw new RuntimeException("当前检查项有数据，不能删除");
		}
		checkItemDao.deleteCheckItemById(id);
	}

	@Override
	public CheckItem findById(Integer id) {
		return checkItemDao.findById(id);
	}

	@Transactional
	@Override
	public void edit(CheckItem checkItem) {
		checkItemDao.edit(checkItem);
	}
}
